// pages/gallery/gallery.js
const controller=require("../controllers/Controller").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon1:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20211123/912659171965403136.png",
    icon2:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20211123/912659199110938624.png",
    leftTabList:[
      
    ],
    selectIndex:0,     //栏目
    tabSelectIndex:0,   //侧边栏
    imgSelectIndex:0,    //选中图片
    folderIndex:"",       //点击图片文件夹
    imgsList:[],
    selectImgList:[],
    firstPageShow:true,
    tabList:[
      
    ]
  },

  //查看原图
  seeBigImg(){
    let list=[]
    list.push(this.data.selectImgList[this.data.imgSelectIndex].image)
    wx.previewImage({
      urls: list,
    })
  },

  //选中图片
  selectImg(e){
    let index=e.currentTarget.dataset.index
    let list=this.data.selectImgList
    if(this.data.upNum==1){
      this.setData({
        imgSelectIndex:e.currentTarget.dataset.index
      })
    }else{
      list[index].selected=!list[index].selected
      this.setData({
        selectImgList:list
      })
    }
  },


  //获取图库栏目
  getTabList(){
    let value={}
    value.type=4
    controller.selectLabel(value).then(res=>{
      this.setData({
        tabList:res.data
      })
    })
  },

  //切换栏目
  changeTabItem(e){
    this.setData({
      selectIndex:e.currentTarget.dataset.index
    })
    this.getSelectImgs()
  },

  //切换leftTab
  changeLeftTab(e){
    this.setData({
      tabSelectIndex:e.currentTarget.dataset.index,
      folderIndex:"",
      firstPageShow:true
    })
    this.getImgLabelsList()
  },

  //获取图片文件夹列表
  getImgLabelsList(){
    let value={}
    value.type=5
    value.labelId=this.data.leftTabList[this.data.tabSelectIndex].id
    value.series=2
    value.typeId=wx.getStorageSync('typeId')
    controller.selectLabel(value).then(res=>{
      this.setData({
        imgsList:res.data
      })
    })
  },


  //跳转图库二级目录
  toGallerySecondPage(e){
    let index=e.currentTarget.dataset.index
    this.setData({
      folderIndex:index,
      firstPageShow:false
    })
    this.getSelectImgs()
  },

  //获取相应的图片列表
  getSelectImgs(){
    let value={}
    value.labelId=this.data.imgsList[this.data.folderIndex].id
    value.type=5
    value.galleryId=this.data.tabList[this.data.selectIndex].id
    value.series=3
    controller.selectLabel(value).then(res=>{

      if(this.data.upNum!=1){
        res.data.forEach(item=>{
          item.selected=false
        })
      }

      this.setData({
        selectImgList:res.data
      })
    })
  },

  //获取分类标签列表
  getLabelList(){
    let value={}
    value.typeId=wx.getStorageSync('typeId')
    controller.getLabelList(value).then(res=>{
      let list=[{
        id:"",
        name:"全部"
      }]
      this.setData({
        leftTabList:list.concat(res.data)
      })
      this.getImgLabelsList()
    })
  },

  //确定
  subImg(){
    let pages=getCurrentPages()
    let prevPage=pages[pages.length-2]
    if(this.data.upNum==1){
      let galleryImg=this.data.selectImgList[this.data.imgSelectIndex].image
      if(this.data.goodsImg){
        prevPage.setData({
          goodsImg:galleryImg
        })
      }
      if(this.data.bannerImg){
        let list=prevPage.data.swiperImgList
        list.push(galleryImg)
        prevPage.setData({
          swiperImgList:list,
          addSwiperImgList:list
        })
      }
  
      if(this.data.detailImg){
        let list=prevPage.data.detailsImgList
        list.push(galleryImg)
        prevPage.setData({
          detailsImgList:list,
          addDetailsImgList:list
        })
      }
    }else{
      let list=[]
      this.data.selectImgList.forEach(item=>{
        if(item.selected==true){
          list.push(item.image)
        }
      })
      if(this.data.goodsImg){
        prevPage.setData({
          goodsImg:list[0]
        })
      }
      if(this.data.bannerImg){
        let preList=prevPage.data.swiperImgList
        preList=preList.concat(list)
        if(preList.length>this.data.upNum){
          preList=preList.slice(0,parseInt(this.data.upNum))
        }
        prevPage.setData({
          swiperImgList:preList,
          addSwiperImgList:preList
        })
        debugger
      }
  
      if(this.data.detailImg){
        let preList=prevPage.data.detailsImgList
        preList=preList.concat(list)
        if(preList.length>this.data.upNum){
          preList=preList.slice(0,parseInt(this.data.upNum))
        }
        prevPage.setData({
          detailsImgList:preList,
          addDetailsImgList:preList
        })
      }
    }
    wx.navigateBack()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.goodsImg){
      this.setData({
        goodsImg:options.goodsImg
      })
    }
    if(options.bannerImg){
      this.setData({
        bannerImg:options.bannerImg
      })
    }
    if(options.detailImg){
      this.setData({
        detailImg:options.detailImg
      })
    }
    this.setData({
      upNum:options.upNum
    })
    this.getTabList()
    this.getLabelList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})